package org.grill.fatwhacker.tools;

import java.text.NumberFormat;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.grill.fatwhacker.gui.Utils;

public class PointFinderDialog {
	private Shell parent;
	private Shell s;
	
	public PointFinderDialog(Shell parent) {
		this.parent = parent;
	}
	
	private Text caloriesText;
	private Text fatText;
	private Text fiberText;
	
	private Label resultLabel;
	
	public void open() {
		s = new Shell(parent);
		s.setText("PointsFinder");
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		layout.marginWidth = 5;
		layout.marginHeight = 5;
		layout.horizontalSpacing = 5;
		layout.verticalSpacing = 5;
		s.setLayout(layout);
		
		new Label(s, SWT.LEFT).setText("Calories:");
		caloriesText = new Text(s, SWT.BORDER);
		GridData caloriesData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		caloriesData.widthHint  = 50;
		caloriesText.setLayoutData(caloriesData);
		caloriesText.addModifyListener(getModifyListener());
		
		new Label(s, SWT.LEFT).setText("Fat (g):");
		fatText = new Text(s, SWT.BORDER);
		GridData fatData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		fatData.widthHint  = 50;
		fatText.setLayoutData(fatData);
		fatText.addModifyListener(getModifyListener());
		
		new Label(s, SWT.LEFT).setText("Fiber (g):");
		fiberText = new Text(s, SWT.BORDER);
		GridData fiberData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		fiberData.widthHint  = 50;
		fiberData.heightHint = 30;
		fiberText.setLayoutData(fiberData);
		fiberText.addModifyListener(getModifyListener());
		
		new Label(s, SWT.NONE);new Label(s, SWT.NONE);
		
		new Label(s, SWT.LEFT).setText("Points: ");
		resultLabel = new Label(s, SWT.LEFT);
		GridData resultData = new GridData(GridData.GRAB_HORIZONTAL | GridData.HORIZONTAL_ALIGN_FILL);
		resultData.widthHint  = 50;
		resultData.heightHint = 50;
		resultLabel.setLayoutData(fiberData);
		
		Label l = resultLabel;
		Utils.setBoldFont(l);
		
//		s.setSize(300,200); 
		s.pack();
		s.open();
	}

	private ModifyListener getModifyListener() {
		return new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				float calories = getValue(caloriesText);
				float fat = getValue(fatText);
				float fiber = getValue(fiberText);
				
				calories /= 50.0;
				fat /= 12.0;
				if (fiber > 4)
					fiber = 4;
				fiber /= -5;
				
				float val = fiber + calories + fat;
				if (val < 0) val = 0;
				NumberFormat format = NumberFormat.getInstance();
				format.setMaximumFractionDigits(1);
				resultLabel.setText(format.format(val));
			}
		};
	}
	
	private float getValue(Text input) {
		float val = 0;
		try {
			float parseFloat = Float.parseFloat(input.getText());
			if (parseFloat < 0)
				parseFloat = 0;
			return parseFloat;
		} catch (NumberFormatException e) {
			return 0;
		}
	}
}
